package com.atguigu.admin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;

import java.io.File;
import java.io.IOException;

/**
 * @Author: LDeng
 * @Date: 2021-08-29 17:49
 */
//文件上传测试：
@Slf4j
@Controller
public class FormTestController {

    @GetMapping("/form_layouts")
    public String form_layouts(){

        return "form/form_layouts";
    }


    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImage") MultipartFile headerImage,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息：email={},username={},headerImg={},photos={}",
                email,username,headerImage.getOriginalFilename(),photos.length);
        if(!headerImage.isEmpty()){
            //保存文件
            headerImage.transferTo(new File("c:\\tmp\\"+headerImage.getOriginalFilename()));
        }

        if(photos.length>0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    photo.transferTo(new File("c:\\tmp\\"+photo.getOriginalFilename()));
                }
            }
        }
        return "main";
    }
}
